package org.chen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.chen.model.CommentBatch;

import java.time.LocalDateTime;

@Mapper
public interface CommentBatchMapper extends BaseMapper<CommentBatch> {
    /**
     * 插入批次记录
     *
     * @return int
     */
    @Insert("INSERT INTO comment_batch (batch_no, total_count, success_count, failure_count, " +
            "comment_content, nickname, email, website_url, status, create_time, update_time) " +
            "VALUES (#{batchNo}, #{totalCount}, #{successCount}, #{failureCount}, " +
            "#{commentContent}, #{nickname}, #{email}, #{websiteUrl}, #{status}, #{createTime}, #{updateTime})")
    int insert(CommentBatch batch);

    /**
     * 根据批次号查询
     */
    @Select("SELECT * FROM comment_batch WHERE batch_no = #{batchNo}")
    @Results({
            @Result(property = "batchNo", column = "batch_no"),
            @Result(property = "totalCount", column = "total_count"),
            @Result(property = "successCount", column = "success_count"),
            @Result(property = "failureCount", column = "failure_count"),
            @Result(property = "commentContent", column = "comment_content"),
            @Result(property = "websiteUrl", column = "website_url"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    CommentBatch selectByBatchNo(String batchNo);

    /**
     * 更新批次状态
     */
    @Update("UPDATE comment_batch SET status = #{status}, update_time = #{updateTime} " +
            "WHERE batch_no = #{batchNo}")
    void updateStatus(@Param("batchNo") String batchNo,
                      @Param("status") String status,
                      @Param("updateTime") LocalDateTime updateTime);

    // 在 CommentBatchMapper 中添加更新方法
    @Update("""
        UPDATE comment_batch 
        SET nickname = #{batch.nickname},
            email = #{batch.email},
            website_url = #{batch.websiteUrl},
            comment_content = #{batch.commentContent},
            status = #{batch.status},
            success_count = #{batch.successCount},
            failure_count = #{batch.failureCount},
            update_time = NOW()
        WHERE batch_no = #{batch.batchNo}
    """)
    void updateBatch(@Param("batch") CommentBatch batch);

    @Update("""
        UPDATE comment_batch 
        SET nickname = #{batch.nickname},
            email = #{batch.email},
            website_url = #{batch.websiteUrl},
            comment_content = #{batch.commentContent},
            update_time = NOW()
        WHERE batch_no = #{batch.batchNo}
    """)
    void updateBatchInfo(@Param("batch") CommentBatch batch);

    @Update("""
        UPDATE comment_batch 
        SET status = #{status},
            update_time = NOW()
        WHERE batch_no = #{batchNo}
    """)
    void updateBatchForResume(
            @Param("batchNo") String batchNo,
            @Param("status") String status
    );
}
